package com.dgut.ibeacon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.ibeacon.entity.AjaxResult;
import com.dgut.ibeacon.entity.Comment;
import com.dgut.ibeacon.service.IBookshelfService;
import com.dgut.ibeacon.service.ICommentService;
import com.dgut.ibeacon.service.IRoleService;
import com.dgut.ibeacon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuec
 * @since 2020-10-14
 */
@RestController
@RequestMapping("//comment")
public class CommentController {

    @Autowired
    ICommentService commentService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IBookshelfService bookshelfService;
    /**
     * 创建评论
     * @param bookshelfId   书架ID号
     * @param bookId    图书ID号
     * @param content   评论内容
     * @return
     */
    @ResponseBody
    @PostMapping("/createComment")
    public AjaxResult createComment(@RequestParam("bookshelfId") int bookshelfId,
                                    @RequestParam("bookId") long bookId,
                                    @RequestParam("content") String content){
        if(bookshelfService.getById(bookshelfId).getIfOpenComment()==false){
            return AjaxResult.error("该书架的评论区处于关闭状态！");
        }
        Comment comment =new Comment();
        comment.setUserId(ShiroUtils.getUserId());
        comment.setBookshelfId(bookshelfId);
        comment.setBookId(bookId);
        comment.setContent(content);
        if (commentService.save(comment)==true){
            return AjaxResult.success("已发表评论！");
        }else {
            return AjaxResult.error("评论发表失败！");
        }
    }

    /**
     * 删除评论
     * @param commentId
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteComment")
    public AjaxResult deleteComment(long commentId){
        Comment comment=commentService.getById(commentId);
        if(comment==null){
            return AjaxResult.error("评论不存在");
        }
        /**
         * 如果评论非自己所写，不可删除
         * 管理员也可删除评论信息
         */
        boolean isAdmin=roleService.getById(ShiroUtils.getUserId()).getRole().equals("admin");
        if(comment.getUserId()==ShiroUtils.getUserId()||isAdmin==true){
            commentService.removeById(commentId);
            return AjaxResult.success("评论已删除");
        }else {
            return AjaxResult.error("无权限删除");
        }
    }

    /**
     * 根据书id号查询评论(分页，按点赞量排)
     * @param bookId
     * @return
     */
    @ResponseBody
    @GetMapping("/listComments")
    public AjaxResult listComments(@RequestParam("bookId") long bookId,
                                   @RequestParam(value = "index",defaultValue = "0") int index,
                                   @RequestParam(value = "size",defaultValue = "15") int size){
        IPage<Comment> page=new Page<>(index,size);
        page=commentService.page(page,new QueryWrapper<Comment>().gt("book_id",bookId).orderByDesc("get_likes"));
        return AjaxResult.success(page);
    }


    @ResponseBody
    @GetMapping("/incLike")
    public AjaxResult incLike(long commentId){
        //todo
        return AjaxResult.success();
    }

    @ResponseBody
    @GetMapping("/decLike")
    public AjaxResult decLike(long commentId){
        //todo
        return AjaxResult.success();
    }





}
